import java.sql.*;
import java.util.Scanner;

/**
 * 功能:修改用户密码
 * SQL语句:UPDATE userinfo SET password='871016' WHERE username='Tony'
 * 数据库查询确认: SELECT * FROM userinfo;
 */
public class JDBCDemo2 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root"
            );
            Statement statement = connection.createStatement();
            /*
                接收用户输入
                1.请输入用户名: Tony
                2.请输入新密码: 19871016
             */
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username = scanner.nextLine();
            System.out.println("请输入新密码:");
            String password = scanner.nextLine();
            //字符串拼接SQL
            //字符串变量占位符: "+变量名+"
            String sql = "UPDATE userinfo SET password='"+password+"' WHERE username='"+username+"'";
            int i = statement.executeUpdate(sql);
            if (i > 0){
                System.out.println("密码修改成功");
            }else{
                System.out.println("密码修改失败");
            }
            connection.close();

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}


